package com.epfresh.supplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.OrderMinusDetailActivity;
import com.epfresh.supplier.entity.MinusRecordBean;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class MinusRecordAdapter extends BaseAdapter implements View.OnClickListener{

    public Context context;

    public List<MinusRecordBean.ContentBean.ListBean> groupList;


    private AdapterView.OnItemClickListener onItemClickListener;

    public MinusRecordAdapter(Context context , List<MinusRecordBean.ContentBean.ListBean> list) {
        this.context = context;
        this.groupList = list;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return groupList != null ?groupList.size():0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_record_minus_child, null, false);
            vh.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            vh.order_no = (TextView) convertView.findViewById(R.id.order_no);
            vh.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
            vh.iv_promotion = (ImageView) convertView.findViewById(R.id.iv_promotion);
            vh.tv_activity_promotion =  convertView.findViewById(R.id.tv_activity_promotion);
            vh.tv_activity_second = convertView.findViewById(R.id.tv_activity_second);
            vh.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            vh.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            //vh.tv_unit = (TextView) convertView.findViewById(R.id.tv_unit);
            vh.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            vh.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            vh.tv_minus_money = (TextView) convertView.findViewById(R.id.tv_minus_money);
            vh.tv_minus_number = (TextView) convertView.findViewById(R.id.tv_minus_number);
            vh.ll_order_child = convertView.findViewById(R.id.ll_order_child);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        MinusRecordBean.ContentBean.ListBean orders = groupList.get(groupPosition);
        vh.order_no.setText("减免退编号：" + orders.getJmtNo());
        ImageLoader.getInstance().displayImage(orders.getProductImgUrl(), vh.iv_logo);
        String promotionMarkUrl = orders.getMarkUrl();
        ImageLoader.getInstance().displayImage(promotionMarkUrl, new ImageViewAware(vh.iv_promotion), BaseApplication.getTagOptions(promotionMarkUrl) , null, null, null);
        //vh.tv_unit.setText(orders.getProductPriceUnit()+"("+orders.getProductPackageSize()+")");
        vh.tv_number.setText(orders.getProductPriceUnit()+"   "+"X" + orders.getProductCount());
        vh.tv_total.setText(orders.getProductTotalPrice() + "元");
        vh.tv_minus_money.setText("减免退金额："+orders.getJmtTotalPrice()+"元");
        vh.tv_order_type.setText(orders.getJmtRefundStatusTitle());
        if(orders.getJmtProductCount()<=0){
            vh.tv_minus_number.setVisibility(View.GONE);
        }else{
            vh.tv_minus_number.setVisibility(View.VISIBLE);
            vh.tv_minus_number.setText("退货："+orders.getJmtProductCount()+orders.getProductChargeUnit());
        }
        if (2==(orders.getSalesType())) {//预售
            vh.tv_type.setVisibility(View.VISIBLE);
            String html="<font color=\"#00ffffff\">【预售】</font>"+orders.getProductTitle();
            vh.tv_title.setText(Html.fromHtml(html));
        } else {//现售
            vh.tv_type.setVisibility(View.GONE);
            vh.tv_title.setText(orders.getProductTitle());
        }


        String promotionType = orders.getPromotionType();
        if ("1".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.VISIBLE);
            vh.tv_activity_second.setVisibility(View.GONE);
        }else  if ("2".equals(promotionType)) {//1促销2秒杀
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.VISIBLE);
        }else {
            vh.tv_activity_promotion.setVisibility(View.GONE);
            vh.tv_activity_second.setVisibility(View.GONE);
        }

/*

        if(orders.getPromotionTypeTitle()!=null&&"秒杀".equals(orders.getPromotionTypeTitle())){
            vh.tv_promotion.setText("秒杀");
            vh.tv_promotion.setVisibility(View.VISIBLE);

            vh.tv_promotion.setBackgroundResource(R.drawable.seckill_tag_bg);
        }else if(orders.getPromotionTypeTitle()!=null&&"促销".equals(orders.getPromotionTypeTitle())){
            vh.tv_promotion.setText("促销");
            vh.tv_promotion.setVisibility(View.VISIBLE);
            vh.tv_promotion.setBackgroundResource(R.drawable.promotion_tag_bg);
        }else {
            vh.tv_promotion.setVisibility(View.GONE);
        }
*/

        vh.ll_order_child.setTag(R.id.item_key_position, orders.getId());
        vh.ll_order_child.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof String) {
            String id = (String) tag;
            switch (v.getId()) {
                case R.id.ll_order_child:
                    Intent detail = new Intent(context, OrderMinusDetailActivity.class);
                    detail.putExtra("id", id);
                    context.startActivity(detail);
                    break;
            }
        }
    }


    public class ViewHolder {
        TextView order_no;
        ImageView iv_logo;
        TextView tv_type;
        ImageView iv_promotion;
/*        TextView tv_promotion;*/
        TextView tv_activity_promotion,tv_activity_second;
        TextView tv_title;
        //TextView tv_unit;
        TextView tv_order_type;

        //holder.setText(R.id.tv_order_type, bean.getJmtRefundStatusTitle());

        TextView tv_number;
        TextView tv_total,tv_minus_number,tv_minus_money;
        View ll_order_child;
    }
}
